package HtServer.RedisTools;

import HtServer.meeting.MeetingItem;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import tools.Configure;

import java.time.Duration;
import java.util.Set;

import static HtServer.meeting.MeetingItems._radonBase64;

public class RedisToos {
    static RedisToos redisToos = null;
//    Jedis jedis = null;
    private static JedisPool jedisPool;
    private RedisToos()
    {
//         jedis = new Jedis("localhost", 6379);

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(50);  // 设置最大连接数
        poolConfig.setMaxIdle(5);    // 设置最大空闲连接数
        poolConfig.setMinIdle(1);    // 设置最小空闲连接数
        poolConfig.setTestOnBorrow(true);  // 获取连接时检查其有效性

//        poolConfig.setMaxTotal(128);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);

        poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
        poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
        poolConfig.setNumTestsPerEvictionRun(3);
        poolConfig.setBlockWhenExhausted(true);

        String port_ = Configure.getInstance().getProperties().getProperty("redis.port","6379");
        int port = Integer.parseInt(port_);
        String host = Configure.getInstance().getProperties().getProperty("redis.url","localhost");
        String password = Configure.getInstance().getProperties().getProperty("redis.password","Ht123321");
        String username = Configure.getInstance().getProperties().getProperty("redis.username","htadmin");
        jedisPool = new JedisPool(poolConfig, host, port,username,password);




    }
    public static RedisToos getInstance()
    {
        if (redisToos == null)
        {
            redisToos = new RedisToos();
        }
        return redisToos;
    }

    public Jedis getJedis() {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.select(0);
            return jedis;
        } catch (Exception e) {
            System.err.println("Error while accessing Redis: " + e.getMessage());
        }

        return null;

    }




    public static void main(String[] argc)
    {

        long t1 = System.currentTimeMillis();

        RedisToos redisToos = RedisToos.getInstance();
        redisToos.getJedis().flushDB();




//
//        for (int i = 0; i<200;i++)
//        {
//            MeetingItem mi_t = new MeetingItem();
//            mi_t.setId("id"+i);
//            mi_t.setName("name"+i);
//            mi_t.setDept("随机部门"+(i/20));
//            mi_t.setImage(_radonBase64());
//            mi_t.setContents( "经营主体是我国经济活动的主要参与者、就业机会的主要提供者、技术进步的主要推动者。围绕“激发各类经营主体活力”，今年的《政府工作报告》部署了一系列重点任务：深入实施国有企业改革深化提升行动，全面落实促进民营经济发展壮大的意见及配套举措……政策持续加力，经营主体开足马力。"+i);
//            mi_t.saveMeetingItem2Redis();
//        }

// 定义匹配模式
        String pattern = "meetingitem:*";
// 使用KEYS命令获取匹配的键列表
        Set<String> keys = redisToos.getJedis().keys(pattern);

        // 遍历并输出匹配的键
        for (String key : keys) {
            System.out.println("Matching key: " + key);
            System.out.println("Matching value: " + redisToos.getJedis().get(key));


        }

        long t2 = System.currentTimeMillis();
        System.out.println("time:"+(t2-t1)/1000);


    }
}
